<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.appName"
          placeholder="请输入应用名称"
          maxlength="30"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formSearch.param.date"
          @change="dateChange"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-warning"
          @click="download"
          v-if="$checkPermission(108)"
          >导出日志</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="appName" label="应用名称"></el-table-column>
        <el-table-column prop="errorType" label="应用描述">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.errorType"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.errorType }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          prop="url"
          label="请求URL"
        ></el-table-column>
        <el-table-column
          prop="ip"
          label="请求IP地址"
          width="140"
        ></el-table-column>
        
        <el-table-column
          prop="addTime"
          label="操作时间"
          width="160"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios.post('/admin/error/page', this.formSearch).then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data.list
          this.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    dateChange(val) {
      // 时间范围发生变化
      if (val) {
        this.formSearch.param.start = val[0]
        this.formSearch.param.stop = val[1]
      } else {
        this.formSearch.param.start = ''
        this.formSearch.param.stop = ''
      }
    },
    download() {
      this.$axios({
        url: '/logs/error/export',
        method: 'GET',
        params: this.formSearch,
        headers: { ContentType: 'application/octet-stream;charset=uft-8;' },
        responseType: 'blob',
      }).then((res) => {
        const reader = new FileReader()
        reader.readAsDataURL(res)
        reader.onload = function (e) {
          const a = document.createElement('a')
          a.download = '错误日志'
          a.href = e.target.result
          a.click()
        }
      })
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$router.push({
            path: '/logCenter/error/detail',
            query: { id: row.id },
          })
          break
      }
    },
  },
}
</script>
